package com.jspx.sioc.aop;

import com.jspx.boot.EnvFactory;
import com.jspx.sioc.BeanFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.xml.ws.Endpoint;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenyuan on 2015-7-8.
 * 开启单独端口发布soap
 *
 <bean id="aopEndpointPublishServices" class="com.jspx.sioc.aop.AopEndpointPublishServices" singleton="false" >
 <string name="host">http://xxxxxxxxxxxxxxxxxxxxxxx</string>
 </bean>
  这种方式不能公用端口host
需要，tomcat的扩展包支持
 http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.35/bin/extras/catalina-ws.jar
 */
public class AopEndpointPublishServices extends AbstractAopBean {
    private static final Logger log = LoggerFactory.getLogger(AopEndpointPublishServices.class);
    private static boolean isRun = false;

    private Map<String,String> serviceMap = new HashMap<String, String>();
    public void setServiceMap(Map<String, String> serviceMap) {
        this.serviceMap = serviceMap;
    }
    private String host;
    public void setHost(String host) {
        this.host = host;
    }

    @Override
    public String init() throws Exception {
        if (isRun) return ERROR;
        isRun = true;
        BeanFactory beanFactory = EnvFactory.getBeanFactory();
        for (String key:serviceMap.keySet())
        {
            String service = serviceMap.get(key);
            log.info("WebService open " + host + "/" + key);
            Endpoint.publish(host + "/" + key, beanFactory.getBean(service));
        }
        return SUCCESS;
    }

    @Override
    public String destroy() throws Exception {
        isRun = false;
        return SUCCESS;
    }


}
